//
//  Photo.m
//  UI09_flim
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Photo.h"
#import "scrollview.h"
@interface Photo()<UIScrollViewDelegate>

@property (nonatomic, retain) scrollview *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, assign) NSInteger flag;


@end




@implementation Photo
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self createSubViews];
    }
    return self;
    
}


    
    
    




- (void)createSubViews {
    

    self.scrollView = [[scrollview alloc] initWithFrame:self.bounds];
    self.scrollView.backgroundColor = [UIColor greenColor];
    [self addSubview: _scrollView];
    [_scrollView release];
    
   
    self.scrollView.delegate = self;
    
    self.scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    

    
    
   
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) / 5 * 4, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) / 5 * 1)];
    [self addSubview:_pageControl];
    
    
    [_pageControl release];
    
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)pageAction:(UIPageControl *)pageControl {
    
    
    
 
    self.scrollView.contentOffset = CGPointMake((pageControl.currentPage + 1) * CGRectGetWidth(self.bounds), 0);
    
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (self.scrollView.contentOffset.x >= self.bounds.size.width * (self.imageArray.count - 1)) {
        [self.scrollView setContentOffset:CGPointMake(self.bounds.size.width, 0) animated:NO];
    }
    
    
    if (self.scrollView.contentOffset.x <= 0) {
        [self.scrollView setContentOffset:CGPointMake(self.bounds.size.width * (self.imageArray.count - 2), 0) animated:NO];
    }
    
    
    
    
    self.pageControl.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds) - 1;
    
    
    if (_flag != self.pageControl.currentPage) {
        
        UIScrollView *tempScro = [self.scrollView.subviews objectAtIndex:_flag + 1];
        tempScro.zoomScale = 1.0;
    }
    
    _flag = self.pageControl.currentPage;
    
    
    
}




- (void)setImageArray:(NSArray<UIImage *> *)imageArray {
    
    if (_imageArray != imageArray) {
        
        [_imageArray release];
        _imageArray = [imageArray retain];
    }
    

    [self.scrollView addPictures:_imageArray];
    
    self.pageControl.numberOfPages = _imageArray.count - 2;
    
    
}





@end
